//
//  AddTodoView.swift
//  ToDo4
//
//  Created by Jeyi Zhou on 2025/10/26.
//

import SwiftUI

struct AddTodoView: View {
    @ObservedObject var todoManager: TodoManager
    @Environment(\.dismiss) private var dismiss
    
    @State private var title = ""
    @State private var description = ""
    @State private var selectedQuadrant: Quadrant = .urgentImportant
    
    private var isFormValid: Bool {
        !title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
    }
    
    var body: some View {
        VStack(spacing: 20) {
            // 标题栏
            HStack {
                Text("添加新任务")
                    .font(.title2)
                    .fontWeight(.bold)
                Spacer()
                Button("关闭") {
                    dismiss()
                }
                .buttonStyle(.plain)
            }
            
            Divider()
            
            // 任务标题
            VStack(alignment: .leading, spacing: 8) {
                Text("任务标题 *")
                    .font(.headline)
                TextField("请输入任务标题", text: $title)
                    .textFieldStyle(.roundedBorder)
            }
            
            // 任务描述
            VStack(alignment: .leading, spacing: 8) {
                Text("任务描述")
                    .font(.headline)
                TextEditor(text: $description)
                    .frame(height: 100)
                    .overlay(
                        RoundedRectangle(cornerRadius: 8)
                            .stroke(Color.gray.opacity(0.3), lineWidth: 1)
                    )
                    .overlay(
                        Group {
                            if description.isEmpty {
                                Text("请输入任务描述（可选）")
                                    .foregroundColor(.gray)
                                    .padding(.horizontal, 8)
                                    .padding(.vertical, 12)
                            }
                        },
                        alignment: .topLeading
                    )
            }
            
            // 四象限选择
            VStack(alignment: .leading, spacing: 12) {
                Text("选择象限 *")
                    .font(.headline)
                
                LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 2), spacing: 12) {
                    ForEach(Quadrant.allCases, id: \.self) { quadrant in
                        QuadrantSelectionCard(
                            quadrant: quadrant,
                            isSelected: selectedQuadrant == quadrant,
                            onTap: {
                                selectedQuadrant = quadrant
                            }
                        )
                    }
                }
            }
            
            Spacer()
            
            // 按钮
            HStack(spacing: 12) {
                Button("取消") {
                    dismiss()
                }
                .buttonStyle(.bordered)
                .frame(maxWidth: .infinity)
                
                Button("添加任务") {
                    addTodo()
                }
                .buttonStyle(.borderedProminent)
                .frame(maxWidth: .infinity)
                .disabled(!isFormValid)
            }
        }
        .padding()
        .frame(minHeight: 400)
    }
    
    private func addTodo() {
        let newTodo = TodoItem(
            title: title.trimmingCharacters(in: .whitespacesAndNewlines),
            description: description.trimmingCharacters(in: .whitespacesAndNewlines),
            quadrant: selectedQuadrant
        )
        
        todoManager.addTodo(newTodo)
        dismiss()
    }
}

#Preview {
    AddTodoView(todoManager: TodoManager())
}
